package com.zzttwl.locktcp.serviceImpl;

import com.zzttwl.locktcp.entity.Record;
import com.zzttwl.locktcp.service.ParseService;
import io.netty.channel.ChannelHandlerContext;
import org.apache.log4j.Logger;

public class ParseServiceImpl implements ParseService {

    //静态log
    protected Logger logger = Logger.getLogger(ParseServiceImpl.class);

    private static String type = "1";//1蓝牙开锁 2 指纹开锁 3密码开锁 4临时密码开锁 5钥匙开锁 6常开报警 7异常开锁
    private static String status = "1";//
    private static String person = "test";
    private static String time = "20180709234055";
    private static String mcu_uuid = "123456789";
    private static String dataType = "1";//1表示固定密码开锁，2表示临时密码开锁，3表示指纹开锁，4表示蓝牙开锁  5表示钥匙开门  如果是报警 1表示门未关好，2表示撬门开锁,3表示电量不足
    private static String state = "1";//1表示开门记录 2表示报警记录

    /**
     * 校验并解析
     */
    public void parseMsg(ChannelHandlerContext ctx, String msg) {
        if (msg != null && isSuccCheckSum(msg)) {
            String fingId = null;
            if (msg.substring(4, 6).equals("30")) {
                if (msg.substring(8, 9).equals("1")) {
                    type = "蓝牙开锁";
                    state = "1";
                    dataType = "4";
                } else if (msg.substring(8, 9).equals("2")) {
                    type = "指纹开锁";
                    state = "1";
                    dataType = "3";
                    fingId = msg.substring(msg.length() - 2, msg.length());
                } else if (msg.substring(8, 9).equals("3")) {
                    type = "密码开锁";
                    state = "1";
                    dataType = "1";
                } else if (msg.substring(8, 9).equals("4")) {
                    type = "临时密码开锁";
                    state = "1";
                    dataType = "2";
                } else if (msg.substring(8, 9).equals("5")) {
                    type = "钥匙开锁";
                    state = "1";
                    dataType = "5";
                } else if (msg.substring(8, 9).equals("6")) {
                    type = "常开报警";
                    dataType = "1";
                    state = "2";
                } else if (msg.substring(8, 9).equals("7")) {
                    type = "异常开锁";
                    dataType = "2";
                    state = "2";
                } else if (msg.substring(8, 9).equals("8")) {
                    type = "电量不足报警";
                    dataType = "3";
                    state = "2";
                }

                if (msg.substring(9, 10).equals("4")) {
                    status = "0100";
                } else if (msg.substring(9, 10).equals("5")) {
                    status = "0101";
                } else if (msg.substring(9, 10).equals("6")) {
                    status = "0110";
                } else if (msg.substring(9, 10).equals("7")) {
                    status = "0111";
                } else if (msg.substring(9, 10).equals("C")) {
                    status = "1100";
                } else if (msg.substring(9, 10).equals("D")) {
                    status = "1101";
                } else if (msg.substring(9, 10).equals("E")) {
                    status = "1110";
                } else if (msg.substring(9, 10).equals("F")) {
                    status = "1111";
                }
//			}
//			else if(msg.substring(4,6).equals("37")){
                String year = Integer.parseInt(msg.substring(10, 14), 16) + "";
                String month = Integer.parseInt(msg.substring(14, 16), 16) + "";
                if (month.length() == 1) {
                    month = "0" + month;
                }
                String day = Integer.parseInt(msg.substring(16, 18), 16) + "";
                if (day.length() == 1) {
                    day = "0" + day;
                }

                String hour = Integer.parseInt(msg.substring(18, 20), 16) + "";
                if (hour.length() == 1) {
                    hour = "0" + hour;
                }

                String min = Integer.parseInt(msg.substring(20, 22), 16) + "";
                if (min.length() == 1) {
                    min = "0" + min;
                }

                String sec = Integer.parseInt(msg.substring(22, 24), 16) + "";
                if (sec.length() == 1) {
                    sec = "0" + sec;
                }


                time = year + month + day + hour + min + sec;
                mcu_uuid = msg.substring(24, msg.length() - 2);

                String param = "mcu_uuid=" + mcu_uuid + "&type=" + dataType + "&time=" + time + "&status=" + state;
                if (fingId != null) {
                    param += "&finger_id=" + fingId;
                }
                String requestResult = HttpRequest.sendGet("http://127.0.0.1:8080/insertOpenLockRecord", param);
                logger.info(requestResult);
            }
        }
    }

    @Override
    public Record getDatatoRecorddata(String[] strs) {
        return null;
    }

    //校验包的校验和是否正确
    @Override
    public boolean isSuccCheckSum(String dataPacket) {
        boolean isSuccCheckSum = false;
        String dataCheckSum = checkSum(dataPacket.substring(4, 6), dataPacket.substring(8, dataPacket.length()));
        String dataTag = dataPacket.substring(6, 8);
        if (dataCheckSum.toUpperCase().equals(dataTag)) {
            isSuccCheckSum = true;
        }
        return isSuccCheckSum;
    }

    //16进制字符串转字节数组 参数 1.标识字节 2.数据字节 得到字节数组校验码
    public static String checkSum(String tagStr, String dataStr) {
        String data = tagStr + dataStr;
        if (data == null || data.equals("")) {
            return "";
        }
        int total = 0;
        int len = data.length();
        int num = 0;
        if (len % 2 == 1) {
            data = data + "0";
        }
        len = data.length();
        while (num < len) {
            String s = data.substring(num, num + 2);
            System.out.println(s);
            total += Integer.parseInt(s, 16);
            num = num + 2;
        }
        /**
         * 用256求余最大是255，即16进制的FF
         */
        int mod = total % 255;
        String hex = Integer.toHexString(mod);
        len = hex.length();
        // 如果不够校验位的长度，补0,这里用的是两位校验
        if (len < 2) {
            hex = "0" + hex;
        }
        return hex;
    }

    //16进制字符串转字节数组
    public static byte[] parseHexStringToBytes(String contentStr) {
        byte[] contentByte = new byte[contentStr.length() / 2];
        for (int i = 0; i < contentByte.length; ++i) {
            contentByte[i] = Long.decode("0x" + contentStr.substring(i * 2, 2 + i * 2)).byteValue();
        }
        return contentByte;
    }

}
